﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using MainController.Hardware.PLC.MEGMEET.Datas.Groups;
using ParamSetCollection.General.Parameters.Normal;
using ParamSetCollection.General.Logs;
using ParamSetCollection.General.ParamTable;

namespace MainController.Hardware.PLC.MEGMEET.Datas
{
    class DataGroup
    {
        #region============================== 变量 ====================================
        //错误消息
        public delegate void OnErrorHandler(string msg);
        public event OnErrorHandler OnError;

        //修改动作记录
        //public event OnChangeMsgHandler OnChangeMsg;



        protected string m_code = "";


        protected bool m_msgLock = false;

        protected Groups.ParamGroup bitsGroup = null;

        protected Groups.ParamGroup wordsGroup = null;

        protected Groups.ParamGroup shortGroup = null;

        protected Groups.ParamGroup dwGroup = null;




        protected List<string> msgList = new List<string>();//消息列表
        protected List<Groups.ParamGroup> groupList = new List<Groups.ParamGroup>();
        #endregion=====================================================================


        #region============================== 属性 ====================================
        public string Code { get { return m_code; } }
        #endregion=====================================================================


        #region============================= 内部函数 ================================
        private void EventBinding()
        {

            foreach (Groups.ParamGroup item in groupList)
            {
                //item.OnChangeMsg += OnParamChangeMsg;
            }


        }


        #endregion====================================================================


        #region============================= 保护函数 ================================
        #endregion====================================================================


        #region============================= 公开函数 ================================
        public DataGroup(string code, string baseCode, ParamSetCollection.General.ParamTable.ParamGroupUnit table, string path, ushort[] dataBuf)
        {
            //if (table.bitParamDic != null && table.bitParamDic.Count > 0)
            //{
            //    //bitsGroup = new ParamGroup(baseCode, index, table.paramGroupInfo.bitAddrList, table.paramGroupInfo.bitCount, ParamGroup.DataType.Bool, table.bitParamPosiDic, path, data, dataType);
            //    bitsGroup = new BitsGroup(code, baseCode,  table.bitParamDic, path, dataBuf);
            //    groupList.Add(bitsGroup);
            //}

            //if (table.shortParamDic != null && table.shortParamDic.Count > 0)
            //{
            //    shortGroup = new ShortGroup(code, baseCode,  table.shortParamDic, path, dataBuf);
            //    groupList.Add(shortGroup);
            //}



            //if (table.dwParamDic != null && table.dwParamDic.Count > 0)
            //{
            //    dwGroup = new DWGroup(code, baseCode,  table.dwParamDic, path, dataBuf);
            //    groupList.Add(dwGroup);
            //}



            this.m_code = code;


            EventBinding();
        }

        public List<CommUnit> InitCtx(Dictionary<string, bool> boolDic, Dictionary<string, ushort> ushortDic)
        {
            List<CommUnit> commList = new List<CommUnit>();
            commList.AddRange(bitsGroup.InitCtx(boolDic));
            commList.AddRange(wordsGroup.InitCtx(ushortDic));

            return commList;
        }


        //更新参数，用于plc参数监控
        public void InputNewDatas(ushort[] data)
        {

            foreach (Groups.ParamGroup item in groupList)
            {

                item.InputNewDatas(data);
            }
        }



        //获取参数修改指令
        public List<CommUnit> GetParamSetting(List<ParamUnit> paramList)
        {
            List<CommUnit> cmdList = new List<CommUnit>();

            List<ParamUnit> _params = new List<ParamUnit>();
            foreach (ParamUnit item in paramList)
            {
                string[] _str = null;
                _str = item.Code.Split('.');
                if (_str[1] == this.m_code)
                {
                    ParamUnit pu = (ParamUnit)item.Clone();
                    pu.Code = _str[2];
                    //item.Code = _str[2];
                    _params.Add(pu);
                }
            }

            if (_params.Count > 0)
            {

                foreach (Groups.ParamGroup item in groupList)
                {
                    cmdList.AddRange(item.GetParamSetting(ref _params));//cmdList.AddRange(item.GetParamSetting(ref paramList));
                }
            }

            return cmdList;
        }



        public List<ParamUnit> GetSpecRemoteParam()
        {
            List<ParamUnit> buf = new List<ParamUnit>();
            foreach (Groups.ParamGroup item in groupList)
            {
                buf.AddRange(item.GetParamList());
            }

            return buf;
        }

        //获取初始化指令
        public List<CommUnit> GetInitCmdList()
        {
            List<CommUnit> cmdList = new List<CommUnit>();


            foreach (Groups.ParamGroup item in groupList)
            {
                cmdList.AddRange(item.GetInitCmdList());
            }

            return cmdList;
        }

        public bool InitUIParam(ParamUnit pu)
        {
            bool rs = false;
            string[] str = pu.Code.Split('.');
            for (int i = 0; i < groupList.Count; i++)
            {
                if (groupList[i].InitUIParam(pu) == true)
                {
                    rs = true;
                    break;
                }
            }
            return rs;
        }



        public void ReduceParamRange(ParamUnit pu)
        {

            bool rs = false;
            string[] str = pu.Code.Split('.');
            for (int i = 0; i < groupList.Count; i++)
            {
                groupList[i].ReduceParamRange(pu);


            }

        }

        public Dictionary<string, List<ParamUnit>> GetRemoteDatas()
        {
            Dictionary<string, List<ParamUnit>> dataBuf = new Dictionary<string, List<ParamUnit>>();
            dataBuf.Add(m_code, new List<ParamUnit>());
            for (int i = 0; i < groupList.Count; i++)
            {
                dataBuf[m_code].AddRange(groupList[i].GetRemoteDatas());
            }

            return dataBuf;
        }
        #endregion====================================================================


        #region============================= 消息响应 ================================
        protected void OnParamChangeMsg(ParamRenewLogMsgUnit actLogUnit)
        {

            //OnChangeMsg?.Invoke(actLogUnit);
        }


        #endregion====================================================================

    }
}
